Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

274
Visualizações
React js | Conditional rendering

I have a problem with conditional rendering in reactjs.

Problem

If an environment is prod, then <Sentry.ErrorBoundary> tag should be loaded.

Is there a better way instead of using if with sentrytag and else without sentrytag?

Code:
ReactDOM.render(
    <Provider store={store}>
        {environment == 'PROD' && <Sentry.ErrorBoundary> }
            <Experiment>
                <Root />
            </Experiment>
            {environment == 'PROD' && </Sentry.ErrorBoundary> }
    </Provider>, document.getElementById('root'));
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Is there a better way instead of using if with sentrytag and else without sentrytag

Yea you could define a variable with the 'wrapper' element, or an 'empty' React.Fragment if the element isn't needed:

const wrapper = (environment === 'PROD') ? Sentry.ErrorBoundary : React.Fragment;

<Provider store={store}>
    <wrapper>
        <Experiment>
            <Root />
        </Experiment>
    </wrapper>
</Provider>

Small working demo, just to give the idea.

The Error component is the Sentry.ErrorBoundary in OP's example.

Press the button to toggle the 'error' state to enable/disable the wrapper.

const Error = (props) => (
    <div>
        <h1>{'Omg; error!'}</h1>
        {props.children}
    </div>
);

const Example = () => {

    const [ err, setErr ] = React.useState(true);
    const Wrapper = (err) ? Error : React.Fragment;
    
    return (
        <div>
            <Wrapper>
                Hello!
            </Wrapper>
            <br /><br /><br />
            <button onClick={() => setErr(!err)}>Toggle</button>
        </div>
    )
}

ReactDOM.render(<Example />, document.getElementById("app"));
<script src="https://unpkg.com/react@18/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"></script>

<div id="app"></div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda